package com.gitee.wsl.doc.soup.ext

import com.fleeksoft.ksoup.nodes.Document
import com.gitee.wsl.ext.string.nullIfBlank


fun Document.extractTitle(): String? = (
        title().nullIfBlank()
            ?: select("title").text().nullIfBlank()
            ?: select("meta[name=title]").attr("content").nullIfBlank()
            ?: select("meta[property=og:title]").attr("content").nullIfBlank()
            ?: select("meta[name=twitter:title]").attr("content").nullIfBlank()
        )?.cleanTitle()?.nullIfBlank()

fun Document.extractCanonicalUrl(): String? = (
        select("link[rel=canonical]").attr("abs:href").nullIfBlank()
            ?: select("meta[property=og:url]").attr("content").nullIfBlank()
            ?: select("meta[name=twitter:url]").attr("content").nullIfBlank()
        )?.removeWhiteSpace()?.nullIfBlank()

// fun Document.extractPaginationUrl(baseUrl: HttpUrl?, nextOrPrev: String): HttpUrl? = (
//        select("link[rel=$nextOrPrev]").attr("abs:href").nullIfBlank()
//        )?.removeWhiteSpace()?.nullIfBlank()
//    ?.let { relativeUrl -> baseUrl?.resolve(relativeUrl) ?: relativeUrl.toHttpUrlOrNull() }

fun Document.extractDescription(): String? = (
        select("meta[name=description]").attr("content").nullIfBlank()
            ?: select("meta[property=og:description]").attr("content").nullIfBlank()
            ?: select("meta[name=twitter:description]").attr("content").nullIfBlank()
        )?.removeWhiteSpace()?.nullIfBlank()

fun Document.extractSiteName(): String? = (
        select("meta[property=og:site_name]").attr("content").nullIfBlank()
            ?: select("meta[name=application-name]").attr("content").nullIfBlank()
        )?.removeWhiteSpace()?.nullIfBlank()

fun Document.extractThemeColor(): String? =
    select("meta[name=theme-color]").attr("content").nullIfBlank()

fun Document.extractPublishedAt(): String? = (
        select("meta[itemprop=dateCreated]").attr("content").nullIfBlank()
            ?: select("meta[property=article:published_time]").attr("content").nullIfBlank()
        )?.removeWhiteSpace()?.nullIfBlank()

fun Document.extractModifiedAt(): String? = (
        select("meta[itemprop=dateModified]").attr("content").nullIfBlank()
            ?: select("meta[property=article:modified_time]").attr("content").nullIfBlank()
        )?.removeWhiteSpace()?.nullIfBlank()

fun Document.extractKeywords(): List<String> =
    select("meta[name=keywords]").attr("content")
        .removeWhiteSpace()
        .removePrefix("[")
        .removeSuffix("]")
        .split("\\s*,\\s*".toRegex())
        .filter { it.isNotBlank() }

// fun Document.extractFaviconUrl(baseUrl: HttpUrl?): HttpUrl? {
//    val allPossibleIconElements = listOf(
//        select("link[rel~=apple-touch-icon]"),
//        select("link[rel~=apple-touch-icon-precomposed]"),
//        select("link[rel~=icon]"),
//        select("link[rel~=ICON]"),
//    )
//    return findLargestIcon(allPossibleIconElements.flatten())
//        ?.let { baseUrl?.resolve(it) ?: it.toHttpUrlOrNull() }
//        ?: baseUrl?.newBuilder()?.encodedPath("/favicon.ico")?.build()
//}
//
// fun Document.extractImageUrl(baseUrl: HttpUrl?): HttpUrl? = (
//        // Twitter Cards and Open Graph images are usually higher quality, so rank them first.
//        select("meta[name=twitter:image]").attr("content").nullIfBlank()
//            ?: select("meta[property=og:image]").attr("content").nullIfBlank()
//            // image_src or thumbnails are usually low quality, so prioritize them *after* article images.
//            ?: select("link[rel=image_src]").attr("href").nullIfBlank()
//            ?: select("meta[name=thumbnail]").attr("content").nullIfBlank()
//        )?.let { baseUrl?.resolve(it) ?: it.toHttpUrlOrNull() }
//
// fun Document.extractFeedUrl(baseUrl: HttpUrl?): HttpUrl? = (
//        select("link[rel=alternate]").select("link[type=application/rss+xml]").attr("href").nullIfBlank()
//            ?: select("link[rel=alternate]").select("link[type=application/atom+xml]").attr("href").nullIfBlank()
//        )?.let { baseUrl?.resolve(it) ?: it.toHttpUrlOrNull() }
//
// fun Document.extractAmpUrl(baseUrl: HttpUrl?): HttpUrl? =
//    select("link[rel=amphtml]").attr("href").nullIfBlank()
//        ?.let { baseUrl?.resolve(it) ?: it.toHttpUrlOrNull() }
//
// fun Document.extractVideoUrl(baseUrl: HttpUrl?): HttpUrl? =
//    select("meta[property=og:video]").attr("content").nullIfBlank()
//        ?.let { baseUrl?.resolve(it) ?: it.toHttpUrlOrNull() }

//internal fun findLargestIcon(iconElements: List<Element>): String? =
//    iconElements.maxByOrNull { parseSize(it.attr("sizes")) }?.attr("abs:href")?.nullIfBlank()

